package com.ruoyi.common.core.page

import com.ruoyi.common.core.text.Convert.toInt
import com.ruoyi.common.utils.ServletUtils.getParameter
import com.ruoyi.common.utils.ServletUtils.getParameterToBool

/**
 * 表格数据处理
 *
 * @author ruoyi
 */
object TableSupport {
    /**
     * 当前记录起始索引
     */
    private const val PAGE_NUM = "pageNum"

    /**
     * 每页显示记录数
     */
    private const val PAGE_SIZE = "pageSize"

    /**
     * 排序列
     */
    private const val ORDER_BY_COLUMN = "orderByColumn"

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    private const val IS_ASC = "isAsc"

    /**
     * 分页参数合理化
     */
    private const val REASONABLE = "reasonable"
    private val pageDomain: PageDomain
        /**
         * 封装分页对象
         */
        get() {
            val pageDomain = PageDomain()
            pageDomain.pageNum = toInt(getParameter(PAGE_NUM), 1)
            pageDomain.pageSize = toInt(getParameter(PAGE_SIZE), 10)
            pageDomain.orderByColumn = getParameter(ORDER_BY_COLUMN)
            getParameter(IS_ASC)?.let { pageDomain.setIsAsc(it) }
            pageDomain.reasonable = getParameterToBool(REASONABLE)
            return pageDomain
        }

    fun buildPageRequest(): PageDomain {
        return pageDomain
    }
}
